package com.xinli.xinlifm.view.fragment.find;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.find.FindCircleBean;
import com.xinli.xinlifm.http.bean.find.FindZhuBean;
import com.xinli.xinlifm.mvp.iview.MvpView;
import com.xinli.xinlifm.mvp.model.SuperModel;
import com.xinli.xinlifm.view.activity.find.FindSecondActivity;
import com.xinli.xinlifm.view.adapter.find.FindPagerAdapter;
import com.xinli.xinlifm.view.adapter.find.ZhuGridAdapter;
import com.xinli.xinlifm.view.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   发现
 * Created by zz on 16-10-12.
 */

public class FindFragment extends BaseFragment<SuperModel> implements MvpView<FindCircleBean>, ViewPager.OnPageChangeListener,Handler.Callback {

    private SimpleAdapter simpleAdapter;
    private SimpleAdapter simpleAdapter2;
    private Handler handler = new Handler(this);
    private int[] img = {
            R.mipmap.kuaile,
            R.mipmap.beishang,
            R.mipmap.gudu,
            R.mipmap.yiqiliao,
            R.mipmap.jianya,
            R.mipmap.wuliao,
            R.mipmap.fanzao,
            R.mipmap.gandong,
            R.mipmap.mimang
    };

     private int[] img_chang = {
            R.mipmap.shuiqian,
            R.mipmap.lvxing,
            R.mipmap.sanbu,
            R.mipmap.zuoche,
            R.mipmap.duchu,
            R.mipmap.shilian,
            R.mipmap.shimian,
            R.mipmap.suibian,
            R.mipmap.wuliao
    };



    private String[] content ={
            "快乐","悲伤","孤独","已弃疗","减压","无奈",
            "烦躁","感动","迷茫"
    };
    private String[] content_chang ={
            "睡前","旅行","散步","坐车","独处","失恋",
            "失眠","随便","无聊"
    };
    private GridView xinGridView;
    private GridView changGridView;
    private ViewPager findPager;
    private LinearLayout layoutPoints;
    private GridView zhuGridView;
    private ZhuGridAdapter zhuAdapter;
    private List<FindZhuBean.DataBean> dataBeen;

    @Override
    public SuperModel getModel() {
        return new SuperModel();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find,container,false);
        initView(view);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        getModel().getFindCircleData();
        getModel().getFindZhuData();
        initEvent();
        return view;
    }

    private void initEvent() {
        xinGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.find_grid_text);
                String trim = textView.getText().toString().trim();
                Intent intent = new Intent(getActivity(), FindSecondActivity.class);
                intent.putExtra("tag",trim);
                intent.putExtra("type",1);
                getActivity().startActivity(intent);
            }
        });

        changGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.find_grid_text);
                String trim = textView.getText().toString().trim();
                Intent intent = new Intent(getActivity(), FindSecondActivity.class);
                intent.putExtra("tag",trim);
                intent.putExtra("type",1);
                getActivity().startActivity(intent);
            }
        });

        zhuGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FindZhuBean.DataBean dataBean = dataBeen.get(position);

                Intent intent = new Intent(getActivity(), FindSecondActivity.class);
                intent.putExtra("id",dataBean.getId());
                intent.putExtra("type",2);
                getActivity().startActivity(intent);
            }
        });
    }

    private void initView(View view) {
        //////////两gridView的设置///////////
        xinGridView = ((GridView) view.findViewById(R.id.find_xin_gridView));
        changGridView = ((GridView) view.findViewById(R.id.find_chang_gridView));

        List<Map<String,Object>> list = new ArrayList<>();
        getListMap(list,img,content);

        List<Map<String,Object>> list2 = new ArrayList<>();
        getListMap(list2,img_chang,content_chang);

        simpleAdapter = new SimpleAdapter(getActivity(),list,R.layout.find_grid_item,new String[]{"img","content"},new int[]{R.id.find_grid_img,R.id.find_grid_text});
        simpleAdapter2 = new SimpleAdapter(getActivity(),list2,R.layout.find_grid_item,new String[]{"img","content"},new int[]{R.id.find_grid_img,R.id.find_grid_text});
        xinGridView.setAdapter(simpleAdapter);
        changGridView.setAdapter(simpleAdapter2);

        ////////////viewPager设置///////////////
        findPager = ((ViewPager) view.findViewById(R.id.find_pager));
        findPager.addOnPageChangeListener(this);
        layoutPoints = ((LinearLayout) view.findViewById(R.id.find_linear_points));

        for (int i = 0; i < 5; i++) {
            ImageView point = new ImageView(getActivity());
            point.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.white_point));
            point.setPadding(5,5,5,5);
            layoutPoints.addView(point);
        }
        ImageView childAt = (ImageView) layoutPoints.getChildAt(0);
        childAt.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.checked_point));
        ////////////////第3个主播gridView///////////////////////////
        zhuGridView = ((GridView) view.findViewById(R.id.find_zhu_gridView));

    }

    /**
     * 创建出对应的list<Map>集合
     * @param list
     */
    private void getListMap(List<Map<String, Object>> list,int img[],String[] title) {
        Map<String,Object> map =null;
        for (int i = 0; i < img.length; i++) {
            map = new HashMap<>();
            map.put("img",img[i]);
            map.put("content",title[i]);
            list.add(map);
        }
    }
    @Subscribe
    @Override
    public void showView(FindCircleBean data) {
        List<FindCircleBean.DataBean> dataBeanList = data.getData();
        findPager.setAdapter(new FindPagerAdapter(getActivity(), dataBeanList));
        findPager.setCurrentItem(1);
        ///
        handler.sendEmptyMessageDelayed(1,5000);
    }
    @Subscribe
    public void showZhuView(FindZhuBean data){
        dataBeen = data.getData();
        zhuAdapter = new ZhuGridAdapter(getActivity(), dataBeen);
        zhuGridView.setAdapter(zhuAdapter);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        if(positionOffset==0){
            if(position==0){
                findPager.setCurrentItem(5,false);
            }else if(position ==6){
                findPager.setCurrentItem(1,false);
            }
        }
    }

    private void resetPoint() {
        for (int i = 0; i < layoutPoints.getChildCount(); i++) {
            ImageView imageView = (ImageView) layoutPoints.getChildAt(i);
            imageView.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.white_point));
        }
    }

    @Override
    public void onPageSelected(int position) {
        //4 0 1 2 3 4 0
        //0 1 2 3 4 5 6
        if(position==0){
            resetPoint();
            ((ImageView) layoutPoints.getChildAt(4)).setImageDrawable(getActivity().getResources().getDrawable(R.drawable.checked_point));
        }else if(position ==6){
            resetPoint();
            ((ImageView) layoutPoints.getChildAt(0)).setImageDrawable(getActivity().getResources().getDrawable(R.drawable.checked_point));
        }else {
            resetPoint();
            ((ImageView) layoutPoints.getChildAt(position-1)).setImageDrawable(getActivity().getResources().getDrawable(R.drawable.checked_point));
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        handler.removeMessages(1);
        handler =null;
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                findPager.setCurrentItem(findPager.getCurrentItem()+1);
                handler.sendEmptyMessageDelayed(1,5000);
                break;
        }
        return true;
    }
}
